package com.twu.biblioteca;

public abstract class Item {
    protected final String name;
    protected final String year;
    protected boolean checkedOut;

    public Item(String name, boolean checkedOut, String year) {
        this.name = name;
        this.checkedOut = checkedOut;
        this.year = year;
    }

    protected Item() {
        name = null;
        year = null;
    }

    public boolean available() {
        return !checkedOut;
    }

    public boolean checkOut() {
        if (checkedOut) {
            return false;
        } else {
            checkedOut = true;
            return true;
        }
    }

    public boolean checkIn() {
        if (checkedOut) {
            checkedOut = false;
            return true;
        } else {
            return false;
        }
    }

    public String toFormattedString(String format) {
        StringBuilder ret = new StringBuilder();
        if (this.checkedOut)
            return ret.append("").toString();
        else {
            return ret.append(String.format(format, fields())).append("\n").toString();
        }
    }

    public boolean matches(String itemToBeReturned) {
        return itemToBeReturned.equals(this.name);
    }

    public abstract Object[] fields();
}
